<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDepts('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-tree
              class="departments-tree"
              :data="treeData"
              :props="defaultProps"
              :default-expand-all="true"
              @node-click="handleNodeClick"
            >
              <template v-slot="{ data }">
                <el-row
                  type="flex"
                  style="width: 100%"
                  justify="space-between"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDepts(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDepts(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              v-if="data && !data.children"
                              @click.native="delDepts(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="isEdit ? '编辑组织架构' : '新增组织架构'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!--
          使用数据回显，显示数据覆盖原因：子组件没有销毁，所以每次都不会created
          只需要在关闭弹窗关闭之前，将子组件销毁即可，每次显示弹框，子组件被重新创建，肯定会执行 created
         -->
          <dept-dialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-list="updateList"
            @close="closeFn"
          /></el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 导入查询企业的部门列表API
import { getDepartments, delDepartment } from '@/api/departments'
// 将封装的数组 -> 树形 方法按需导入
import { tranListToTreeData } from '@/utils'
// 引入封装的子组件
import DeptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制添加或编辑弹框展示
      showDialog: false,
      parentId: '',
      // 是否编辑
      isEdit: false,
      originList: []
    }
  },
  // 在页面打开的时候就调用这个方法将数据铺设在页面上
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取部门列表
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      this.treeData = tranListToTreeData(res.data.depts)

      /*
      因为 map 内部只返回了一个对象，所以可以直接把对象返回
      但是函数体不能写成键值对形式，所以使用括号包裹，括号中的代码会被视为一个整体
      */
      this.originList = res.data.depts.map(item => ({
        code: item.code,
        id: item.id,
        name: item.name,
        pid: item.pid
      }))
      console.log(this.originList)
    },

    handleNodeClick(data) {
      console.log(data)
    },

    // 添加子部门
    addDepts(id) {
      this.parentId = id
      this.showDialog = true
      // 点击新增时将isEdit设置为false
      this.isEdit = false
    },

    // 编辑部门
    editDepts(id) {
      this.parentId = id
      this.showDialog = true
      // 点击编辑设置为true
      this.isEdit = true
    },

    // 添加部门功能
    updateList() {
      // 父级关闭弹框
      this.showDialog = false
      // 重新渲染添加的数据
      this.getDepartments()
    },

    // 点击取消
    closeFn() {
      this.showDialog = false
    },

    // 点击删除部门
    async delDepts(id) {
      // 绘制询问框
      const comfirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 用户点击取消，给用户提示
      if (comfirmRes === 'cancel') return this.$message.info('您取消了删除')
      // 点击确定，调用API
      const res = await delDepartment(id)
      // 删除成功以后，给用户提示，渲染列表
      this.$message.success(res.message)
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 0.417rem 1.25rem;
}

.departments-tree {
  margin: 0.5rem 0rem;
}

.tab-pane {
  padding: 0.833rem 3.333rem;

  .company {
    margin-left: 0.5rem;
    font-weight: bold;
    font-size: 0.583rem;
  }

  .department-header {
    height: 2.083rem;
    display: flex;
    align-items: center;
    border-bottom: 0.042rem solid #ccc;
  }
}
</style>
